package datastream.api.datasink;

import org.apache.flink.streaming.api.datastream.DataStreamSink;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.SinkFunction;
import org.apache.flink.streaming.api.functions.source.SourceFunction;
import org.apache.log4j.Logger;

public class UserDefinedSink {
    private static Logger logger = Logger.getLogger(UserDefinedSink.class);
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);

        DataStreamSource<String> source = env.addSource(new SourceFunction<String>() {
            @Override
            public void run(SourceContext<String> ctx) throws Exception {
                ctx.collect("数据 1");
                Thread.sleep(1000);

                ctx.collect("数据 2");
                Thread.sleep(1000);

                ctx.collect("数据 3");
            }

            @Override
            public void cancel() {

            }
        });

        DataStreamSink<String> sink = source.addSink(new SinkFunction<String>() {
            @Override
            public void invoke(String value, Context context) throws Exception {
                logger.error("日志：" + value);
            }
        });

        env.execute();
    }
}
